package com.gxl.butt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.dto.bo.Ip;
import com.gxl.butt.dto.bo.Token;
import com.gxl.butt.dto.bo.Topic;
import com.gxl.butt.dto.bo.User;
import com.gxl.butt.dto.qo.ParamBean;
import com.gxl.butt.dto.qo.QueryBean;
import com.gxl.butt.service.IIPService;
import com.gxl.butt.service.ITokenService;
import com.gxl.butt.service.IkafkaOperatorService;
import com.gxl.butt.utils.IPUtil;
import com.gxl.butt.utils.JSONUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutionException;

import static com.gxl.butt.value.ErrorMsg.*;
import static com.gxl.butt.value.StaticValue.USER_ID;


/**
 * Backend controller used to monitor and restart the KClient.
 *
 * @author guoxiaolin
 * @since Aug 21, 2015
 *
 */
@Api(value = "wxsys operator",tags = "wxsys operator")
@RestController
@RequestMapping(value = "/wxsys")
public class WXController {

	private  Logger log = LoggerFactory.getLogger(WXController.class);

	@Reference(interfaceClass = IkafkaOperatorService.class,timeout = 10000,check = false)
	private IkafkaOperatorService kafkaOperatorService;

	@Reference(interfaceClass = IIPService.class,timeout = 10000,check = false)
	private IIPService iipService;

	@Reference(interfaceClass = ITokenService.class,timeout = 10000,check = false)
	private ITokenService iTokenService;

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;

	@ApiOperation(value="创建主题TOPIC", notes="创建主题TOPIC")
	@RequestMapping(value = "/createTopic",method = RequestMethod.POST)
	public JSONObject createTopic(@RequestBody Topic topic) {

		log.info("topic >>>>" + topic.toString());
		JSONObject jsonObject = null;
		try {
			jsonObject = kafkaOperatorService.createTopic(topic);
		} catch (ExecutionException e) {
			return JSONUtils.getRoot(-2,e.getMessage(), STR_IS_EMPTY);
		} catch (InterruptedException e) {
			return JSONUtils.getRoot(-3,e.getMessage(), STR_IS_EMPTY);
		}

		return jsonObject;
	}

	@ApiOperation(value="获取主题列表", notes="根据用户openid获取主题列表")
	@RequestMapping(value = "/getTopicsbyopenid",method = RequestMethod.POST)
	public JSONObject getTopicList(@RequestBody QueryBean obj){

		String openid = obj.getOpenid();
		JSONObject tokenlist = kafkaOperatorService.getTopicsbyopenid(obj.getPageNo(),obj.getRowCount(),openid);
		return tokenlist;

	}

	@ApiOperation(value="修改主题备注", notes="根据用户openid修改主题备注")
	@RequestMapping(value = "/updateRemark",method = RequestMethod.POST)
	public JSONObject updateRemark(@RequestBody ParamBean obj){

		JSONObject data = obj.getObj();
		JSONObject redata = kafkaOperatorService.updataRemark(data.getString("remark"), data.getString("topicid"), data.getString("openid"));
		return redata;

	}

	@ApiOperation(value="修改主题运行状态", notes="修改主题运行状态")
	@RequestMapping(value = "/updateTopicStatus",method = RequestMethod.POST)
	public JSONObject updateTopicStatus(@RequestBody ParamBean obj){

		JSONObject data = obj.getObj();
		JSONObject redata = kafkaOperatorService.updateTopicStatus(data.getLong("topicid"), data.getInteger("topicstatus"));
		return redata;

	}

	@ApiOperation(value="获取topic近七日消息数据量", notes="获取topic近七日消息数据量")
	@RequestMapping(value = "/getTopicReportInWeek",method = RequestMethod.POST)
	public JSONObject getTopicReportInWeek(@RequestBody ParamBean obj){

		JSONObject data = obj.getObj();
		JSONObject redata = kafkaOperatorService.getTopicReportInWeek(data.getLong("topicid"));
		return redata;

	}

	@ApiOperation(value="删除ip白名单", notes="删除ip")
	@RequestMapping(value = "/delIp",method = RequestMethod.POST)
	public JSONObject delIp(@RequestBody Ip obj){

		Long id = obj.getId();
		JSONObject data = iipService.delIpById(id);
		return  data;

	}

	@ApiOperation(value="添加ip白名单", notes="根据openid,添加ip")
	@RequestMapping(value = "/addIpWithOpenid",method = RequestMethod.POST)
	public JSONObject addIpWithOpenid(@RequestBody Ip obj){

		String openid = obj.getOpenid();
		String[] ip = obj.getIP().split(",");
		if (ip.length > 10){
			return  JSONUtils.getRoot(-2,IP_FORMAT_ERROR,STR_IS_EMPTY);
		}
		for (int i=0 ;i<ip.length;i++){
			if (!IPUtil.isIp(ip[i])){
				return  JSONUtils.getRoot(-1,IP_FORMAT_ERROR,STR_IS_EMPTY);
			}
		}
		JSONObject resdata = iipService.addIpWithOpenid(obj.getIP(),openid);

		return  resdata;

	}

	@ApiOperation(value="查询ip白名单列表", notes="根据openid获取白名单IP列表")
	@RequestMapping(value = "/getipsByOpenid",method = RequestMethod.POST)
	public JSONObject getipsByOpenid(@RequestBody QueryBean obj){

		String openid = obj.getOpenid();

		JSONObject resdata = iipService.getipsByOpenid(obj.getPageNo(),obj.getRowCount(),openid);

		if (resdata.get(JSONUTIL_DATA) == null){

			return  JSONUtils.getRoot(-1,IP_LIST_IS_NULL,STR_IS_EMPTY);

		}

		return  resdata;

	}


	@ApiOperation(value="更新凭证token", notes="更新接口请求凭证Token")
	@RequestMapping(value = "/getToken",method = RequestMethod.POST)
	public JSONObject getToken(@RequestBody Token obj){

		String openid = obj.getOpenid();
		if (openid == null || openid.isEmpty()){
			return JSONUtils.getRoot(-1,USER_LOGOUT,STR_IS_EMPTY);
		}
		return iTokenService.getToken(obj.getOpenid());

	}

	@ApiOperation(value="获取凭证token", notes="获取接口请求凭证Token")
	@RequestMapping(value = "/showToken",method = RequestMethod.POST)
	public JSONObject showToken(@RequestBody Token obj){

		String openid = obj.getOpenid();
		if (openid == null || openid.isEmpty()){
			return JSONUtils.getRoot(-1,OPEN_NOT_NULL,STR_IS_EMPTY);
		}

		return iTokenService.showToken(openid);

	}

	@ApiOperation(value="获取消息总数，日均消息数量", notes="获取消息总数，日均消息数量")
	@RequestMapping(value = "/getSumOfMessage",method = RequestMethod.POST)
	public JSONObject getSumOfMessage(){

		JSONObject resdata = kafkaOperatorService.getSumOfMessage();

		return resdata;

	}

	@ApiOperation(value="TESTTTTT", notes="TESTTTTT")
	@RequestMapping(value = "/TESTTTTT",method = RequestMethod.GET)
	public JSONObject TESTTTTT(){

		JSONObject resdata = new JSONObject();
		resdata.put("errorcode",0);

		return JSONUtils.getRoot(0,"TRUE",resdata);

	}
}